<script setup>
import { ref, onMounted, watch, computed } from 'vue'
import { useRoute } from 'vue-router'
import GoodsItem from '@/components/GoodsItem.vue'
import { useGetUserOrderService } from '@/api/requsets/index.js'
import { isEmptyExpressionValueDirective } from 'eslint-plugin-vue/lib/utils/index.js'

const route = useRoute()

const activeName = ref(0)

const orderList = ref([])

const render = async (parmas) => {
  const {
    data: {
      list: { data }
    }
  } = await useGetUserOrderService(parmas)
  orderList.value = data
  console.log(orderList.value)
}

onMounted(async () => {
  activeName.value = route.query.dataType
  // await render({ dataType: activeName, page: '1' })
})

watch(activeName, async (value) => {
  await render({ dataType: value, page: '1' })
})
</script>

<template>
  <div class="order">
    <van-nav-bar
      title="我的订单"
      left-arrow
      @click-left="$router.push('/user')"
    />

    <van-tabs
      v-model:active="activeName"
      color="rgba(250, 34, 9)"
      title-active-color="rgba(250, 34, 9)"
    >
      <van-tab title="全部" name="all"></van-tab>
      <van-tab title="待支付" name="payment"></van-tab>
      <van-tab title="代发货" name="delivery"></van-tab>
      <van-tab title="代收货" name="received"></van-tab>
      <van-tab title="待评价" name="comment"></van-tab>
    </van-tabs>

    <div class="order-container">
      <div class="order-card-list" v-if="orderList.length > 0">
        <div
          class="order-card-item"
          v-for="item in orderList"
          :key="item.order_id"
        >
          <div class="title">
            <p>{{ item.create_time }}</p>
            <span>{{ item.state_text }}</span>
          </div>
          <GoodsItem
            v-for="goods in item.goods"
            :key="goods.goods_id"
            mode="order"
            :item="goods"
          >
          </GoodsItem>
          <div class="footer">
            <p>
              共<span>{{
                item.goods.reduce((sum, item) => (sum += item.total_num), 0)
              }}</span
              >件商品，总金额
              <span>￥{{ item.total_price }}</span>
            </p>
            <van-button size="small" v-if="item.state_text !== '待取消'"
              >申请取消</van-button
            >
            <p v-if="item.state_text === '待取消'" style="color: #777">
              取消申请中
            </p>
          </div>
        </div>
      </div>
      <van-empty
        v-else
        image="https://fastly.jsdelivr.net/npm/@vant/assets/custom-empty-image.png"
        :image-size="['150px', '100%']"
        description="没有该状态的订单(⊙o⊙)哦"
      />
    </div>
  </div>
</template>

<style lang="less">
.order {
  background-color: #fafafa;

  .order-container {
    padding: 0 10px;
    margin-top: 20px;

    .order-card-list {
      padding: 10px 10px;
      border-radius: 20px;

      .order-card-item {
        background-color: #fff;
        padding: 10px 10px 0 10px;
        border-radius: 20px;
        margin-bottom: 20px;

        .title {
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-size: 14px;

          p {
            color: #777;
          }

          span {
            color: #f9211c;
          }
        }

        .van-card {
          background-color: #fff !important;

          .van-card__title {
            font-weight: normal;
          }

          .van-card__bottom {
            transform: translateY(-20px);
            display: flex;
            //flex-direction: column;
            line-height: var(--van-line-height-md);

            .van-card__price {
              font-weight: normal;
              color: #f9211c;

              .van-card__price-integer {
                font-size: 14px;
                font-weight: normal;

                span {
                  color: #f9211c;
                }
              }
            }

            .van-card__num {
              transform: translateX(20px);
              color: var(--van-card-num-color);
            }
          }

          .van-card__footer {
            p {
              font-size: 14px;
              margin-bottom: 10px;
              span {
                color: #f9211c;
              }
            }
          }
        }
        .footer {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: end;
          p {
            span {
              color: #f9211c;
            }
          }
        }
      }
    }
  }
}
</style>
